package panel.mapPanel
{
	import assists.ButtonUtils;
	import assists.HtmlText;
	import assists.Lang;
	import assists.Scale9Grid;
	import assists.URI;
	import assists.log.Log;
	import assists.resource.ImageResource;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	
	import main.ClientConstants;
	
	import struct.Restaurant;
	import struct.Scenic;
	import struct.ScenicShop;
	
	public class VisitPathBox extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var infoList:Array;
		
		private var centerPointX:Number = 0;
		private var centerPointY:Number = 0;
		
		private var picWid:Number = 0;
		private var picHei:Number = 0;
		
		private var scaleNum:int = 12;
		private var scenicMarkStr:String;
		private var scenicShopMarkStr:String;
		private var eatingResturantMarkStr:String;
		
		private var pathStr:String;
		
		private var mapImg:ImageResource;                     //静态地图
		private var map:BaiduMap;                                  //动态地图
		
		private var _onShow:Function;
		private var _onShowSysRecommend:Function;
		
		private var timeLine:TimeLinePanel;
		
		private var nullBtn:ButtonUtils;
		private var nullBox:Shape;
		
		public var titleString:String;
		
		public function VisitPathBox(stage:Stage,loader:ResourceLoader,lang:Lang,uri:URI)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			this.
				infoList = [];
		}
		
		public function set info(list:Array):void
		{
			infoList = list;
			
			if(list.length> 0){
				//				getCenterPoint();          //注释掉即地图模块提至上一层添加
				//				getImg();                      使用静态地图
				//				addMap();                 //使用百度地图
				
				addBox();
			}else{
				addNullBox();
			}
		}
		
		private function addNullBox():void
		{
			nullBox = new Shape();
			nullBox.graphics.beginFill(0x000000,0);
			nullBox.graphics.drawRect(0,0,_stage.stageWidth,_stage.stageHeight * .82);
			nullBox.graphics.endFill();
			this.addChild(nullBox);
			
			var btnImg:Bitmap = new _loader.uiManager.btnBack as Bitmap;
			
			var grid:Scale9Grid = new Scale9Grid(btnImg.bitmapData,5,5);
			grid.setSize(_stage.stageWidth * .6,_stage.stageHeight * .15);
			grid.width = _stage.stageWidth * .6;
			grid.height = _stage.stageHeight * .15;
			
			nullBtn = new ButtonUtils(grid,clickEvent,HtmlText.formatSizeColor("您当前没有选择自定义路线，点击查看推荐路线",0xFFFF00,18));
			addChild(nullBtn);
			nullBtn.x = _stage.stageWidth * .2;
			nullBtn.y = _stage.stageHeight * .2;
		}
		
		private function clickEvent():void
		{
			if(_onShowSysRecommend is Function){
				_onShowSysRecommend();
			}
		}
		
		private function addBox():void
		{
			timeLine = new TimeLinePanel(_stage,_loader,_lang,_uri);
			timeLine.titleStr = titleString;
			timeLine.showDetail = _onShow;
			timeLine.changeData(infoList);
			this.addChild(timeLine);
			timeLine.x = 0;
			timeLine.y = 0;//_stage.stageHeight * .5;
		}
		
		private function getCenterPoint():void
		{
			var maxX:Number = 0;
			var maxY:Number = 0;
			var minX:Number = 0;
			var minY:Number = 0;
			
			maxX = infoList[0].location[0];
			maxY = infoList[0].location[1];
			minX = infoList[0].location[0];
			minY = infoList[0].location[1];
			
			for(var i:int = 0;i < infoList.length;i++){
				if(infoList[i].location[0] >= maxX){
					maxX = infoList[i].location[0];
				}
				if(infoList[i].location[1] >= maxY){
					maxY = infoList[i].location[1];
				}
				if(infoList[i].location[0] <= minX){
					minX = infoList[i].location[0];
				}
				if(infoList[i].location[1] <= minY){
					minY = infoList[i].location[1];
				}
			}
			
			centerPointX = (maxX + minX) / 2;
			centerPointY = (maxY + minY) / 2;
			
			picWid = (maxX - minX) * 1.1;
			picHei = (maxY - minY) * 1.1;
			
			countDistance();
		}
		
		private function countDistance():void
		{
			var num1:int = int((0.056 - picWid) / .05) + 12;
			var num2:int = int((0.14 - picHei) / .05) + 12;
			
			scaleNum = (num1 < num2)?num1:num2;
			
			if(scaleNum == 0){
				scaleNum = 13;
			}
			
			//			getStyleStr();
		}
		
		private function getStyleStr():void
		{
			scenicMarkStr = "";
			scenicShopMarkStr = "";
			eatingResturantMarkStr = "&markers=color:red%7Clabel:D";
			pathStr = "&path=color:0x0000ff|weight:3";
			
			for(var i:int = 0;i < infoList.length;i++){
				if(infoList[i] is Scenic){
					scenicMarkStr = scenicMarkStr + "&markers=color:blue%7Clabel:" + (i + 1) + "%7C" + infoList[i].location[0] + "," + infoList[i].location[1];
				}
				if(infoList[i] is ScenicShop){
					scenicShopMarkStr = scenicShopMarkStr + "&markers=color:yellow%7Clabel:" + (i + 1) + "%7C" + infoList[i].location[0] + "," + infoList[i].location[1];
				}
				if(infoList[i] is Restaurant){
					eatingResturantMarkStr = eatingResturantMarkStr +"&markers=color:red%7Clabel:" + (i + 1) + "%7C" + infoList[i].location[0] + "," + infoList[i].location[1];
				}
				pathStr = pathStr + "|" + infoList[i].location[0] + "," + infoList[i].location[1];
			}
		}
		
		private function getImg():void
		{
			var url:String = "https://maps.google.com/maps/api/staticmap?" 
				+ "center=" + String(centerPointX) + "," + String(centerPointY) 
				+ "&zoom=" + scaleNum
				+ "&scale=2"
				+ "&size=" + _stage.stageWidth + "x" + _stage.stageHeight * .5
				+  scenicMarkStr + scenicShopMarkStr + eatingResturantMarkStr
				//				+ pathStr
				+ "&key=AIzaSyA9vmhoyxBQFQGP3-TRnUFOmoZaL_vV-9s"
				+ "&language=zh-CN"
				+ "&sensor=true";
			
			mapImg = new ImageResource(_loader,url,ajustThis);
			this.addChild(mapImg);
		}
		
		private function ajustThis():void
		{
			if(_stage){
				mapImg.width = _stage.stageWidth;
				mapImg.height = _stage.stageHeight * .5;
			}
		}
		
		public function set onShowDetail(func:Function):void
		{
			_onShow = func;
		}
		
		public function set onShowSysPath(func:Function):void
		{
			_onShowSysRecommend = func;
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				if(mapImg){
					this.removeChild(mapImg);
					mapImg = null;
				}
				if(timeLine){
					timeLine.clear();
					this.removeChild(timeLine);
					timeLine = null;
				}
				if(nullBtn){
					this.removeChild(nullBtn);
					nullBtn = null;
				}
				if(nullBox){
					this.removeChild(nullBox);
					nullBox = null;
				}
				if(map){
					map.clear();
					map = null;
				}
			}
		}
		
		private function addMap():void
		{
			map = new BaiduMap(_stage,_loader,_lang,_uri);
			map.init(_stage.stageWidth,_stage.stageHeight * .5);
			this.addChild(map);
			
			map.moveToCenter(centerPointX,centerPointY);
			map.changeZom(scaleNum);
			
			//			var pathList:Array = [];
			//			
			//			for(var i:int = 0;i < infoList.length;i++){
			//				if(infoList[i] is Scenic){
			//					map.addLabel(infoList[i].location[0],infoList[i].location[1],ClientConstants.VISIT_TYPE_SCENIC,"1");
			//				}
			//				if(infoList[i] is ScenicShop){
			//					map.addLabel(infoList[i].location[0],infoList[i].location[1],ClientConstants.VISIT_TYPE_FUN,"1");
			//				}
			//				if(infoList[i] is Restaurant){
			//					map.addLabel(infoList[i].location[0],infoList[i].location[1],ClientConstants.VISIT_TYPE_EATING,"1");
			//				}
			//				pathList.push(infoList[i].location);
			//			}
			//			map.addLine(pathList);
		}
	}
}